package com.cfl.usermodule.service.impl;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cfl.model.User;
import com.cfl.usermodule.dao.UserMapper;
import com.cfl.usermodule.service.UserService;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;
	
	public User findUserByName(String name) throws Exception {
		
		try {
			return userMapper.selectByName(name);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public User findUserByNameAndPassword(String name, String password) throws Exception {
		
		try {
			return userMapper.selectByNameAndPassword(name, password);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public Boolean addUser(String name, String password) throws Exception {
		
		try {
			if(StringUtils.isBlank(name) || StringUtils.isBlank(password)) {
				return false;
			}
			User user = new User();
			user.setUid(UUID.randomUUID().toString());
			user.setToken(UUID.randomUUID().toString());
			user.setName(name);
			user.setPassword(password);
			user.setNickName(com.cfl.utils.StringUtils.getRandomString(8));
			userMapper.insert(user);
			return true;
		} catch (Exception e) {
			throw e;
		}
	}
}
